﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Configuration;
using System.Xml;
using DevExpress.XtraBars.Docking2010.Views.Tabbed;
using CDMS.Entities;
using DevExpress.XtraEditors;
using CDMS.CommonForm;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraBars.Ribbon;
using CDMS.Utilities;
using CDMS.Forms;
using DevExpress.XtraBars.Alerter;
using CDMS.Report;
using CDMS.Common;

namespace CDMS
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        SkinManagerEx skinManagerEx = new SkinManagerEx();
        DocumentManagerEx documentManagerEx;

        BackgroundWorker workForAlert = new BackgroundWorker();

        public frmMain()
        {

            InitializeComponent();

            skinManagerEx.InitSkinGallery(ribbonGalleryBarItem1);
            documentManagerEx = new DocumentManagerEx(tabbedView1);
            tabbedView1.DocumentActivated += tabbedView1_DocumentActivated;
            this.Text = Globals.assemblyDescription;



            Timer timer1 = new Timer();
            timer1.Enabled = true;
            timer1.Interval = 2000;
            timer1.Tick += timer1_Tick;


            workForAlert.WorkerSupportsCancellation = true;
            workForAlert.DoWork += new System.ComponentModel.DoWorkEventHandler(workForAlert_DoWork);
            workForAlert.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(workForAlert_RunWorkerCompleted);

        }

        void timer1_Tick(object sender, EventArgs e)
        {
            if (!workForAlert.IsBusy && Program.userName.Length > 0)
                workForAlert.RunWorkerAsync();


        }

        private void workForAlert_DoWork(object sender, DoWorkEventArgs e)
        {
            //Kiểm tra data, ví dụ đếm ra khách hàng nợ

        }
        private void workForAlert_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //Show messagebox hoặc alartcontrol

            //AlertInfo alertInfo = new AlertInfo("TRONGVM", "XIN CHÀO");
            //AlertControl control = new AlertControl();
            //control.FormLocation = AlertFormLocation.BottomRight;
            //control.AutoFormDelay = 7000;
            //control.Show(this, alertInfo);
            //control.AlertClick += control_AlertClick;
        }

        /// <summary>
        /// Khi document duoc chon (kich hoat) thi thay doi gia tri this.Text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tabbedView1_DocumentActivated(object sender, DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e)
        {
            this.Text = string.Format("{0} :: {1}", Globals.assemblyDescription, e.Document.Caption);
        }



        private void ribbon_SelectedPageChanged(object sender, EventArgs e)
        {
            //barStaticItem_Status.Caption = String.Format("|| Bạn đang mở tab {0} || ", ribbon.SelectedPage.Text);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            SplashScreenManager.CloseForm(false);

            Login();

        }
        void Login()
        {

            ribbon.ShowPageHeadersMode = DevExpress.XtraBars.Ribbon.ShowPageHeadersMode.Hide;

            barStaticItem_Username.Visibility = BarItemVisibility.Never;
            barButtonItem_Logout.Visibility = BarItemVisibility.Never;

            foreach (RibbonPage page in ribbon.Pages)
            {
                page.Visible = false;
            }

            Program.userName = "";
            Program.passWord = "";

            if (!Program.isConnected)
            {
                frmConnection fconn = new frmConnection(); //Gọi form Connect
                if (fconn.ShowDialog() == DialogResult.Cancel)
                    return;
            }

            frmLogin f = new frmLogin();
            if (f.ShowDialog() == DialogResult.OK)
            {
                ribbon.ShowPageHeadersMode = DevExpress.XtraBars.Ribbon.ShowPageHeadersMode.Default;
                foreach (RibbonPage page in ribbon.Pages)
                {
                    page.Visible = true;
                }

                barStaticItem_Username.Visibility = BarItemVisibility.Always;
                //barStaticItem_Username.Caption = string.Format("Mã NV: {0} [{1}]!", Program.userName, "Admin");

                barButtonItem_Logout.Visibility = BarItemVisibility.Always;
                barButtonItem_Logout.Caption = string.Format("Logout: ({0})", Program.userName);


                //Thiết lập page "Bán hàng" được chọn (mở)
                ribbon.SelectedPage = ribbon.Pages[1];

                //Gọi màn hình danh sách khách hàng
                mnuCustomerList.PerformClick();

                //Gọi màn hình thông táo nhắc việc
                ShowAlertControl("Nhắc việc", "Công nợ khách hàng (1)", "CongNo");


            }
            else
                Application.Exit();
        }
        void ShowAlertControl(string caption, string text, string tag)
        {
            //Thông tin để thông báo
            AlertInfo alertInfo = new AlertInfo(caption, text);
            alertInfo.Tag = tag;

            //Màn hình thông báo
            AlertControl control = new AlertControl();
            control.FormLocation = AlertFormLocation.BottomRight;
            control.AutoFormDelay = 7000;
            control.ShowPinButton = false;
            control.AutoHeight = true;
            control.Show(this, alertInfo);

            //Sự kiện click vào text thông báo
            control.AlertClick += control_AlertClick;
        }
        void control_AlertClick(object sender, AlertClickEventArgs e)
        {
            if (e.Info.Tag.ToString() == "CongNo")
            {
                MessageBox.Show("Màn hình công nợ sẽ được mở...(Chức năng này đang được xây dựng)");
            }
            else if (e.Info.Tag.ToString() == "HopDong")
            {
                MessageBox.Show("Màn hình hợp đồng hết hạn sẽ được mở...(Chức năng này đang được xây dựng)");
            }
        }


        void CloseTabCurrent()
        {
            Document doc = tabbedView1.ActiveDocument as Document;
            if (doc != null)
                tabbedView1.Controller.Close(doc);
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case (Keys.Control | Keys.F4):
                    CloseTabCurrent();
                    break;
            }
        }
        void ShowForm(XtraUserControl ucControl, ItemClickEventArgs e)
        {
            documentManagerEx.AddTab(ucControl, e.Item.Caption);
        }

        private void mnuUsers_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.Users, e.Item.Caption), e);
        }

        private void mnuProductGroup_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.ProductGroup, e.Item.Caption), e);
        }

        private void mnuCustomerGroup_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.CustomerGroup, e.Item.Caption), e);

        }

        private void mnuCustomerType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.CustomerType, e.Item.Caption), e);
        }

        private void mnuContractType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.ContractType, e.Item.Caption), e);
        }

        private void mnuMemberType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.MemberType, e.Item.Caption), e);
        }

        private void mnuIncomeDocType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.IncomeDocType, e.Item.Caption), e);
        }

        private void mnuSpendDocType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.SpendDocType, e.Item.Caption), e);
        }

        private void mnuPriceType_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.PriceType, e.Item.Caption), e);
        }

        private void mnuContractStatus_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.ContractStatus, e.Item.Caption), e);
        }

        private void mnuQuotationStatus_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.QuotationStatus, e.Item.Caption), e);
        }

        private void mnuOrderStatus_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.OrderStatus, e.Item.Caption), e);
        }

        private void mnuPosition_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.Position, e.Item.Caption), e);
        }

        private void mnuVocative_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.Vocative, e.Item.Caption), e);
        }

        private void mnuMarketSegment_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.MarketSegment, e.Item.Caption), e);
        }

        private void mnuSourceInfo_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.SourceInfo, e.Item.Caption), e);
        }

        private void mnuNationlaty_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.Nationlaty, e.Item.Caption), e);
        }

        private void mnuCity_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.City, e.Item.Caption), e);
        }

        private void mnuVATCode_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.VATCode, e.Item.Caption), e);
        }

        private void mnuSalesArea_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.SalesArea, e.Item.Caption), e);
        }

        private void mnuLimitDebitAge_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.LimitDebitAge, e.Item.Caption), e);
        }

        private void mnuUnit_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.Unit, e.Item.Caption), e);
        }

        private void mnuPaymentWay_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCateogryList(EnumCategory.PaymentWay, e.Item.Caption), e);
        }

        private void mnuCustomerList_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucCustomerList(e.Item.Caption), e);
        }

        private void mnuProductList_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucProductList(e.Item.Caption), e);
        }

        private void barButtonItem_Logout_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FormGlobals.Message_QuestionYesNo("Bạn có thật sự muốn thoát khỏi chương trình?", Globals.assemblyTitle) == DialogResult.Yes)
            {
                documentManagerEx.CloseAllTabs();
                Login();
            }
        }

        private void btnContractList_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowForm(new ucContractList(e.Item.Caption), e);
        }

        private void mnuConnectionSetting_ItemClick(object sender, ItemClickEventArgs e)
        {
            //string demoString = "TRONGVM";
            //frmSQLConnector frm = demoString == string.Empty ? new frmSQLConnector() : new frmSQLConnector(demoString);
            //frm.ShowDialog();
            //ServerModeStrings.SQLConnectionString = frm.GetDataBaseConnectionString();

            frmPreviewReport f = new frmPreviewReport(CDMS.Report.EnumReport.DSKhachHang);
            f.ShowDialog();

        }

        private void mnuSalesList_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void mnuConnectionConfig_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmConnection f = new frmConnection(); //Gọi form Connect
            f.ShowDialog();
        }


    }
}